package com.jyserver.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class Replaces {

    public static String RelacesString(String str, String regex, String newChara, int limit) {
        /**
         * @Description:替换字符串当中的所有符合字符
         * @param str 要替换的字符串
         * @param regex 要替换的字符
         * @param newChara 新字符
         * @param limit 替换位置
         * @author ChenYM
         * @date 2018/7/3  下午 22:03
         * @Return java.lang.String
         */
        String relust = "";
        String[] strs = str.split(regex);
        for (int i = 0; i < strs.length; i++) {
            if ((limit == -1 && i == strs.length - 1) || i == limit) {
                relust = relust + newChara;
            } else {
                relust = relust + strs[i];
            }
            if (i < strs.length - 1) {
                relust = relust + regex;
            }
        }
        if (HelpUtils.isEmpty(relust)) {
            relust = str;
        }
        return relust;
    }


    public static String StringFilter(String str) throws PatternSyntaxException {
        // 只允许字母和数字、中文
        // String regEx="[^a-zA-Z0-9]";
        // 清除掉[]中所有特殊字符
        String regEx = "[`~☆★!@#$%^&*()+=|{}':;,\\[\\]》·.<>/?~！@#￥%……（）_——+|{}【】‘；：”“’。，、？]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim().replace(" ", "").replace("\\", "");
    }

    public static String toLowerCaseFirstOne(String s){
        // @Description:首字母转小写
        if(Character.isLowerCase(s.charAt(0)))
            return s;
        else
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
    }

    public static String toUpperCaseFirstOne(String s){
        // @Description:首字母转大写
        if(Character.isUpperCase(s.charAt(0)))
            return s;
        else
            return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
    }


}
